﻿using System;
using System.Collections;

using IBatisNet.DataAccess;

using MT.eVotePRS.Model;
using MT.eVotePRS.Service;
using MT.eVotePRS.Persistence.Interfaces;

namespace MT.eVotePRS.Service
{
    public class InventoryTxnService
    {
        #region Private Fields
        private static InventoryTxnService _instance = new InventoryTxnService();
        private IDaoManager _daoManager = null;
        private IInventoryTxnDao _txnDao = null;

        #endregion

        #region Constructor

        private InventoryTxnService()
        {
            _daoManager = ServiceConfig.GetInstance().DaoManager;
            _txnDao = _daoManager.GetDao(typeof(IInventoryTxnDao)) as IInventoryTxnDao;

        }

        #endregion

        #region Public methods

        public static InventoryTxnService GetInstance()
        {
            return _instance;
        }

        public object InsertInventoryTxn(InventoryTxn txn)
        {
            object obj = null;

            _daoManager.BeginTransaction();

            try
            {
                obj = _txnDao.InsertInventoryTxn(txn);
                _daoManager.CommitTransaction();
            }
            catch
            {
                _daoManager.RollBackTransaction();
                throw;
            }

            return obj;
        }

        public InventoryTxn QueryLastTxnOfHandset(string handsetId)
        {
            InventoryTxn txn = null;

            _daoManager.OpenConnection();
            txn = _txnDao.QueryLastTxnOfHandset(handsetId);
            _daoManager.CloseConnection();

            return txn;
        }

        public InventoryTxn QueryLastTxnOfStudent(string studentId)
        {
            InventoryTxn txn = null;

            _daoManager.OpenConnection();
            txn = _txnDao.QueryLastTxnOfStudent(studentId);
            _daoManager.CloseConnection();

            return txn;

        }

        public IList GetTxnByHandsetId(string handsetId)
        {
            return _txnDao.GetTxnByHandsetId(handsetId);
        }

        public IList GetTxnByStudentID(string studentId)
        {
            return _txnDao.GetTxnByStudentID(studentId);
        }

        public IList GetAllTxnList()
        {
            return _txnDao.GetAllTxnList();
        }

        public IList GetStudentEventsByStatus(int statusId)
        {
            return _txnDao.GetStudentEventsByStatus(statusId);
        }

        public IList GetHandsetEventsByStatus(int statusId)
        {
            return _txnDao.GetHandsetEventsByStatus(statusId);
        }

        public IList GetStudentEvents(Status status, DateTime dtFrom, DateTime dtTo)
        {
            Hashtable paramHash = new Hashtable();
            paramHash.Add("StatusId", status.Id);
            paramHash.Add("TIME_FROM", dtFrom);
            paramHash.Add("TIME_TO", dtTo);

            return _txnDao.GetStudentEvents(paramHash);
        }

        public IList GetHandsetEvents(Status status, DateTime dtFrom, DateTime dtTo)
        {
            Hashtable paramHash = new Hashtable();
            paramHash.Add("StatusId", status.Id);
            paramHash.Add("TIME_FROM", dtFrom);
            paramHash.Add("TIME_TO", dtTo);

            return _txnDao.GetHandsetEvents(paramHash);
        }

        #endregion
    }
}
